struct Solution {}

fn main() {}

// Definition for a binary tree node.
#[derive(Debug, PartialEq, Eq)]
pub struct TreeNode {
  pub val: i32,
  pub left: Option<Rc<RefCell<TreeNode>>>,
  pub right: Option<Rc<RefCell<TreeNode>>>,
}

impl TreeNode {
  #[inline]
  pub fn new(val: i32) -> Self {
    TreeNode {
      val,
      left: None,
      right: None
    }
  }
}

use std::rc::Rc;
use std::cell::RefCell;
impl Solution {
    pub fn sum_of_left_leaves(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
        let mut ret = 0;
        Self::do_preorder(root, &mut ret);
        ret
    }

    fn do_preorder(root: Option<Rc<RefCell<TreeNode>>>, ret: &mut i32) {
        if root.is_none() {
            return;
        }
        let node = root.unwrap();

        if node.clone().borrow().left.is_some() {
            if Self::is_leaf(node.clone().borrow().left.clone()) {
                *ret += node.clone().borrow().left.clone().unwrap().borrow().val;
            }
            Self::do_preorder(node.clone().borrow().left.clone(), ret);
        }
        Self::do_preorder(node.clone().borrow().right.clone(), ret);
    }

    fn is_leaf(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
        if root.is_none() {
            return false;
        }
        let node = root.unwrap();
        if node.borrow().left.is_none() && node.borrow().right.is_none() {
            return true;
        }
        return false;
    }
}